# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId
from sqlalchemy import func


class LogInfo(db.Model, JsonEncoder):
    __tablename__ = "log"
    id = db.Column(db.String, primary_key = True)
    log = db.Column(db.String)
    create_time = db.Column(db.DateTime, default = func.now())
    account = db.Column(db.String)
    br_count = db.Column(db.Integer)
    color = db.Column(db.String)
    zone_code = db.Column(db.String)
    env_name = db.Column(db.String)
    entrance_types = db.Column(db.String)
    group = db.Column(db.String)
    show_error = db.Column(db.String)

    def __init__(self, log = None, account = None, br_count = 0, color = None, zone_code = None, env_name = None,
                 entrance_types = None, group = None, show_error = '1'):
        self.id = createId('lo')
        self.log = log
        self.account = account
        self.br_count = br_count
        self.color = color
        self.zone_code = zone_code
        self.env_name = env_name
        self.entrance_types = entrance_types
        self.group = group
        self.show_error = show_error

    def to_dict(self):
        return {
            'id': self.id,
            'log': self.log,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'account': self.account,
            'br_count': self.br_count,
            'color': self.color,
            'zone_code': self.zone_code,
            'env_name': self.env_name,
            'entrance_types': self.entrance_types,
            'group': self.group,
            'show_error': self.show_error,
        }
